package com.bangbang.controller;


import com.bangbang.configresult.Result;
import com.bangbang.configresult.ResultEnum;
import com.bangbang.pojo.TbAdvertisingBilling;
import com.bangbang.service.ITbAdvertisingBillingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 广告计费类型表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-08-14
 */
@RestController
@RequestMapping("/groupCourse/tbAdvertisingBilling")
@Api(value = "广告计费类型、涉及增删改查")
public class TbAdvertisingBillingController {

    @Autowired
    private ITbAdvertisingBillingService billingService;


    @ApiOperation("广告类型新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billName", value = "类型名称", required = true, paramType = "query"),
            @ApiImplicitParam(name = "billMethod", value = "计费类型", paramType = "query"),
            @ApiImplicitParam(name = "billDetails", value = "计费详情", paramType = "query"),
    })
    @PostMapping
    public Result insertAdverTise(String billName, String billMethod, String billDetails){
        try {
            TbAdvertisingBilling billing = new TbAdvertisingBilling();
            billing.setBillName(billName).setSparA(billMethod).setSparB(billDetails);
            billingService.insert(billing);
            return Result.buildResult(ResultEnum.SUCCESS).setResult("新增类型成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.buildResult(ResultEnum.ERROR).setResult("新增类型失败");
        }
    }

    @ApiOperation("广告类型删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billId", value = "类型Id", required = true, paramType = "path"),
    })
    @DeleteMapping("/{billId}")
    public Result deleteAdverTise(@PathVariable("billId")Long billId){
        try {
            billingService.removeById(billId);
            return Result.buildResult(ResultEnum.SUCCESS).setResult("删除类型成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.buildResult(ResultEnum.ERROR).setResult("删除类型失败");
        }
    }

    @ApiOperation("广告类型查询展示")
    @GetMapping
    public Result selestAdverTise(){
        try {
            List<TbAdvertisingBilling> billings =  billingService.selestAdverTise();
            return Result.buildResult(ResultEnum.SUCCESS).setResult(billings);
        }catch (Exception e){
            e.printStackTrace();
            return Result.buildResult(ResultEnum.ERROR).setResult("查询类型失败");
        }
    }

    @ApiOperation("广告类型修改回显")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billId", value = "类型Id", required = true, paramType = "path"),
    })
    @GetMapping("/{billId}")
    public Result selestAdverTiseById(@PathVariable("billId")Long billId){
        try {
            TbAdvertisingBilling billing = billingService.selectById(billId);
            return Result.buildResult(ResultEnum.SUCCESS).setResult(billing);
        }catch (Exception e){
            e.printStackTrace();
            return Result.buildResult(ResultEnum.ERROR).setResult("查询类型失败");
        }
    }


    @ApiOperation("广告类型修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bill", value = "类型对象", required = true, paramType = "body"),
    })
    @PutMapping
    public Result updateAdverTiseById(TbAdvertisingBilling bill){
        try {
            billingService.updateById(bill);
            return Result.buildResult(ResultEnum.SUCCESS).setResult("修改类型成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.buildResult(ResultEnum.ERROR).setResult("修改类型失败");
        }
    }

}

